package com.qzsf.backend.controller;

import com.qzsf.backend.common.Result;
import com.qzsf.backend.dto.ForgetRequest;
import com.qzsf.backend.dto.LoginRequest;
import com.qzsf.backend.dto.UserDetailDTO;
import com.qzsf.backend.entity.UserProfile;
import com.qzsf.backend.service.UserService;
import com.qzsf.backend.util.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.Year;
import java.util.Map;

/*
 * @Author: Dennis Chen
 * @CreateTime: 2024/10/30 20:11 周三
 * @Email 1429278462@qq.com
 * @Remark 用户控制器
 * @Version 1.0
 */
@RestController
public class UserController {
    @Autowired
    private UserService userService;
    @GetMapping("quit")
    public Result quit(@RequestHeader("Authorization") String token){
        return userService.quit(token);
    }
    @PostMapping("login")
    public Result login(@RequestBody LoginRequest loginRequest){
        return userService.login(loginRequest);
    }
    @GetMapping("userRole")
    public Result userRole(){
        return userService.getUserRole();
    }
    @GetMapping("users")
    public Result getAllUserDetail(@RequestParam(required = false) String name, @RequestParam(required = false) Year enrollmentYear, @RequestParam(required = false) String faculty, @RequestParam int pageNo, @RequestParam int pageSize) {
        return userService.getAllUserDetail(name,enrollmentYear,faculty,pageNo,pageSize);
    }

    @PostMapping("users")
    public Result createUser(@RequestBody UserDetailDTO userDetailDto){
        return userService.createUser(userDetailDto);
    }

    @DeleteMapping("users/{id}")
    public Result deleteUser(@PathVariable int id){
        return userService.deleteUser(id);
    }

    @PutMapping("users")
    public Result updateUser(@RequestBody UserProfile userProfile){
        return userService.updateUser(userProfile);
    }

    @GetMapping("users/{id}")
    public Result getUserById(@PathVariable int id){
        return userService.getUserProfile(id);
    }

    @PostMapping("users/forget")
    public Result forgetUserPassword(@RequestBody ForgetRequest forgetRequest){
        return userService.forget(forgetRequest);
    }

    @GetMapping("mobile/getBasicProfile")
    public Result getUserBasicProfile(){
        return userService.getUserBasicProfile();
    }

    @GetMapping("mobile/getUserProfile")
    public Result getUserProfile(){
        Map<String,Object> map = ThreadLocalUtil.get();
        int id = (int) map.get("id");
        return userService.getUserProfile(id);
    }

    @PostMapping("mobile/updateUserProfile")
    public Result updateUserProfile(@RequestBody UserProfile userProfile){
        Map<String,Object> map = ThreadLocalUtil.get();
        int id = (int) map.get("id");
        userProfile.setId(id);
        return userService.updateUser(userProfile);
    }
}
